﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SaiPuLSCoffee.model
{
    public class CoffeeMakes
    {
        private static CoffeeMakes instance;
        private CoffeeMakes() { }
        public static CoffeeMakes Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CoffeeMakes();
                }
                return instance;
            }
        }
        public List<WuliaoCoffeeInit> coffeeInits = new List<WuliaoCoffeeInit>();
        /// <summary>
        /// io板是不是在用
        /// </summary>
        public bool CFBol = true;
        /// <summary>
        ///咖啡出餐1 true 出餐 false 没有餐
        /// </summary>
        public bool CFCCBol1 = false;
        /// <summary>
        ///咖啡出餐2 true 出餐 false 没有餐
        /// </summary>
        public bool CFCCBol2 = false;
        ///// <summary>
        ///// 碗1恢复完
        ///// </summary>
        //public bool WHF1Bol = false;
        ///// <summary>
        ///// 碗2恢复完
        ///// </summary>
        //public bool WHF2Bol = false;

        public delegate void TestDelegateInvoke(string guid);

        private void DelegateInvokeMethod(string guid)
        {
            try
            {
                while (!instance.CFBol)
                {
                    Thread.Sleep(100);
                }
                instance.CFBol = false;
                if (instance.coffeeInits.Any(a => a.Guid == guid && a.LeachInit == 1))
                {
                    IOMsg.Instance.AllBol = false;
                    Thread.Sleep(300);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_start);
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_end);
                    Thread.Sleep(300);
                    IOMsg.Instance.AllBol = true;
                    instance.CFBol = true;
                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.cfInit1 * 1000));
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_start);
                    //Thread.Sleep(500);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_end);
                    //Thread.Sleep(300);

                }
                else
                {
                    IOMsg.Instance.AllBol = false;
                    Thread.Sleep(300);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_start);
                    Thread.Sleep(100);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_end);
                    Thread.Sleep(300);
                    IOMsg.Instance.AllBol = true;
                    instance.CFBol = true;
                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.cfInit2 * 1000));
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_start);
                    //Thread.Sleep(500);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_end);
                    //Thread.Sleep(300);
                    //IOMsg.Instance.AllBol = true;
                }

                var coffee = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                coffee.SWInit = 3;
                ClientList clientList2 = new ClientList();
                WuliaoCoffeeInit init = new WuliaoCoffeeInit();
                init.TypeCodeInit = 2;
                init.SWInit = 3;
                init.Guid = guid;
                clientList2.coffeeInit = init;
                clientList2.code = 5;
                if (SocketServer.Instance.CoffeeSocket != null)
                    SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke2(int LeachInit);

        private void DelegateInvokeMethod2(int LeachInit)
        {
            try
            {
                while (!instance.CFBol)
                {
                    Thread.Sleep(100);
                }
                instance.CFBol = false;
                if (LeachInit == 1)
                {
                    IOMsg.Instance.AllBol = false;
                    Thread.Sleep(500);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_start);
                    Thread.Sleep(500);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_end);
                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.cfcxInit1 * 1000));
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_start);
                    Thread.Sleep(500);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ1_end);
                    Thread.Sleep(500);
                    IOMsg.Instance.AllBol = true;
                }
                else
                {
                    IOMsg.Instance.AllBol = false;
                    Thread.Sleep(500);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_start);
                    Thread.Sleep(500);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_end);
                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.cfcxInit2 * 1000));
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_start);
                    Thread.Sleep(500);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.CFJCQ2_end);
                    Thread.Sleep(500);
                    IOMsg.Instance.AllBol = true;
                }
                instance.CFBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }

        /// <summary>
        /// 咖啡左臂制作流程
        /// </summary>
        /// <param name="LeachInit"> 萃取头 1 萃取头1  2 萃取头2</param>
        /// <param name="ClassCode">1 意式  2 冰意式  3 意式搅拌  4 奶搅拌</param>
        /// <param name="guid">制作id</param>
        public void MakingListL(int LeachInit, int ClassCode, string guid)
        {
            try
            {
                switch (LeachInit)
                {
                    case 0:
                        Thread.Sleep(300);
                        if (!instance.coffeeInits.Any(a => a.LeachInit == 1))
                            Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotQW1L_UP);
                        else
                            Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotQW2L_UP);
                        Thread.Sleep(300);
                        if (instance.coffeeInits.Any(a => a.BeanCodeInit == 1))
                            Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotD1L_UP);
                        else if (instance.coffeeInits.Any(a => a.BeanCodeInit == 2))
                            Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotD2L_UP);
                        else
                            Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotD3L_UP);

                        var coffee = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                        coffee.SWInit = 1;
                        ClientList clientList2 = new ClientList();
                        WuliaoCoffeeInit init = new WuliaoCoffeeInit();
                        init.TypeCodeInit = 2;
                        init.SWInit = 1;
                        init.Guid = guid;
                        clientList2.coffeeInit = init;
                        clientList2.code = 5;
                        if (SocketServer.Instance.CoffeeSocket != null)
                            SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));
                        Thread.Sleep(300);
                        Robot1Msg.Instance.RobotBol = false;
                        while (true)
                        {
                            if (!instance.coffeeInits.Any(a => a.LeachInit == 1))
                                Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW1CWCX_Sel);
                            else
                                Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW2CWCX_Sel);
                            Thread.Sleep(300);
                            if (Robot1Msg.Instance.RobotBol)
                            {
                                if (!instance.coffeeInits.Any(a => a.LeachInit == 1))
                                    Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotQW1L_Down);
                                else
                                    Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotQW2L_Down);
                                TestDelegateInvoke2 testDelegate = new TestDelegateInvoke2(DelegateInvokeMethod2);
                                testDelegate.BeginInvoke(instance.coffeeInits.Any(a => a.LeachInit == 1) ? 2 : 1, null, null);
                                break;
                            }
                        }
                        Thread.Sleep(300);
                        Robot1Msg.Instance.RobotBol = false;
                        while (true)
                        {
                            if (!instance.coffeeInits.Any(a => a.LeachInit == 1))
                                Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW1DCQ_Sel);
                            else
                                Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW2DCQ_Sel);
                            Thread.Sleep(300);
                            if (Robot1Msg.Instance.RobotBol)
                            {
                                var coffees = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                                coffees.LeachInit = instance.coffeeInits.Any(a => a.LeachInit == 1) ? 2 : 1;
                                coffees.SWInit = 2;
                                ClientList clientList3 = new ClientList();
                                WuliaoCoffeeInit init1 = new WuliaoCoffeeInit();
                                init1.TypeCodeInit = 2;
                                init1.SWInit = 2;
                                init1.Guid = guid;
                                clientList3.coffeeInit = init1;
                                clientList3.code = 5;
                                if (SocketServer.Instance.CoffeeSocket != null)
                                    SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList3)));
                                break;
                            }
                        }
                        var cof = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                        Robot1Msg.Instance.QKL1(cof.LeachInit, cof.BeanCodeInit);
                        break;
                    default:
                        if (instance.coffeeInits.Any(a => a.SWInit == 4 && a.Guid == guid))
                        {
                            var cf = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                            Thread.Sleep(300);
                            if (cf.LeachInit == 1)
                                Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotXW1L_UP);
                            else
                                Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotXW2L_UP);
                            Thread.Sleep(300);
                            Robot1Msg.Instance.RobotBol = false;
                            while (true)
                            {
                                if (cf.LeachInit == 1)
                                    Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW1CWCX_Sel);
                                else
                                    Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW2CWCX_Sel);
                                Thread.Sleep(300);
                                if (Robot1Msg.Instance.RobotBol)
                                {
                                    if (cf.LeachInit == 1)
                                        Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotXW1L_Down);
                                    else
                                        Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotXW2L_Down);
                                    TestDelegateInvoke2 testDelegate = new TestDelegateInvoke2(DelegateInvokeMethod2);
                                    testDelegate.BeginInvoke(instance.coffeeInits.Any(a => a.LeachInit == 1) ? 1 : 2, null, null);
                                    cf.SWInit = 5;

                                    ClientList clientList3 = new ClientList();
                                    WuliaoCoffeeInit init1 = new WuliaoCoffeeInit();
                                    init1.TypeCodeInit = 2;
                                    init1.SWInit = 5;
                                    init1.Guid = guid;
                                    clientList3.coffeeInit = init1;
                                    clientList3.code = 5;
                                    if (SocketServer.Instance.CoffeeSocket != null)
                                        SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList3)));
                                    break;
                                }
                            }
                            Thread.Sleep(300);
                            Robot1Msg.Instance.RobotBol = false;
                            while (true)
                            {
                                if (cf.LeachInit == 1)
                                    Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW1HFW_Sel);
                                else
                                    Robot1SerialPort.Instance.SerialSend(Robot1Model.Instance.RobotW2HFW_Sel);
                                Thread.Sleep(300);
                                if (Robot1Msg.Instance.RobotBol)
                                {
                                    cf.SWInit = 6;
                                    ClientList clientList3 = new ClientList();
                                    WuliaoCoffeeInit init1 = new WuliaoCoffeeInit();
                                    init1.TypeCodeInit = 2;
                                    init1.SWInit = 6;
                                    init1.Guid = guid;
                                    clientList3.coffeeInit = init1;
                                    clientList3.code = 5;
                                    if (SocketServer.Instance.CoffeeSocket != null)
                                        SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList3)));
                                    break;
                                }
                            }
                        }

                        break;
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 咖啡右臂制作流程
        /// </summary>
        /// <param name="TypeCode"> 订单状态 1，待制作 2，制作中 3，已完成</param>
        /// <param name="ClassCode">1 意式  2 冰意式  3 意式搅拌  4 奶搅拌</param>
        /// <param name="guid">制作id</param>
        public void MakingListR(int TypeCode, int ClassCode, string guid)
        {
            try
            {
                Console.WriteLine(TypeCode + " typecode");
                Console.WriteLine(ClassCode + " ClassCode");
                Console.WriteLine(guid + " guid");
                switch (TypeCode)
                {
                    case 1:
                        bool cqyd = false;
                        int qb = 1;
                        //if (instance.coffeeInits.Any(a => a.TypeCodeInit == 2))
                        //{
                        if (Robot2Msg.Instance.CQWZInit == 1)
                        {
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotCQDYDR_UP);
                            cqyd = true;
                            Thread.Sleep(300);
                            Robot2Msg.Instance.RobotBol = false;
                            Thread.Sleep(300);
                            while (true)
                            {
                                //Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQBWCR_Sel);
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotYDR_Sel);
                                Thread.Sleep(300);
                                if (Robot2Msg.Instance.RobotBol)
                                {
                                    break;
                                }
                            }
                            Thread.Sleep(300);
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotCQDYDR_Down);
                            Thread.Sleep(300);
                        }
                        //  }
                        if (ClassCode == 1)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP1R_UP);
                        else if (ClassCode == 2)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP2R_UP);
                        else if (ClassCode == 3)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP3R_UP);
                        else if (ClassCode == 4)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP4R_UP);
                        Thread.Sleep(300);
                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQQBXR_UP);
                        //if (!instance.CFCCBol1 && WuLiaoCoffeeSel.Instance.Cup1_Sel_Bol)
                        //{
                        //    qb = 1;
                        //    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB1R_UP);
                        //}
                        //else if (!instance.CFCCBol2 && WuLiaoCoffeeSel.Instance.Cup2_Sel_Bol)
                        //{
                        //    qb = 2;
                        //    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB2R_UP);
                        //}
                        ClientList clientList1 = new ClientList();
                        WuliaoCoffeeInit init1 = new WuliaoCoffeeInit();
                        init1.TypeCodeInit = 2;
                        init1.Guid = guid;
                        clientList1.coffeeInit = init1;
                        clientList1.code = 5;
                        if (SocketServer.Instance.CoffeeSocket != null)
                            SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList1)));
                        var cof2 = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                        cof2.TypeCodeInit = 2;
                        Robot2Msg.Instance.RobotBol = false;
                        Thread.Sleep(300);
                        while (true)
                        {
                            //Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQBWCR_Sel);
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQBWCXR_Sel);
                            Thread.Sleep(300);
                            if (Robot2Msg.Instance.RobotBol)
                            {
                                break;
                            }
                        }
                        //Thread.Sleep(300);
                        //Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB1R_Down);
                        //Thread.Sleep(300);
                        //Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQB2R_Down);
                        Thread.Sleep(300);
                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQQBXR_Down);
                        Thread.Sleep(300);
                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotCQDYDR_Down);
                        Thread.Sleep(300);
                        if (ClassCode == 1)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP1R_Down);
                        else if (ClassCode == 2)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP2R_Down);
                        else if (ClassCode == 3)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP3R_Down);
                        else if (ClassCode == 4)
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotP4R_Down);
                        Thread.Sleep(300);
                        if (ClassCode != 4)
                        {

                            if (ClassCode == 2 || ClassCode == 3)
                            {
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJBR_UP);
                                Thread.Sleep(300);
                                Robot2Msg.Instance.RobotBol = false;
                                while (true)
                                {
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDDJBR_Sel);
                                    Thread.Sleep(300);
                                    if (Robot2Msg.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                IceMakingMsg.Instance.AllBol = false;
                                Thread.Sleep(1000);
                                IceMakingSerialPort.Instance.SerialSend(IceMakingModel.Instance.IceMaking2);
                                Thread.Sleep(2000);
                                IceMakingMsg.Instance.AllBol = true;

                            }
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQCQZBR_UP);
                            Thread.Sleep(300);
                            Robot2Msg.Instance.RobotBol = false;
                            while (true)
                            {
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDCQZBR_Sel);
                                Thread.Sleep(300);
                                if (Robot2Msg.Instance.RobotBol)
                                {
                                    break;
                                }
                            }
                            while (true)
                            {
                                if (instance.coffeeInits.Any(a => a.Guid == guid && a.SWInit == 2))
                                {
                                    break;
                                }
                                Thread.Sleep(300);
                            }
                            var cof1 = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                            Thread.Sleep(300);
                            if (cof1.LeachInit == 1)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW1CQR_UP);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW2CQR_UP);
                            Thread.Sleep(300);
                            Robot2Msg.Instance.RobotBol = false;
                            while (true)
                            {
                                if (cof1.LeachInit == 1)
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDFW1R_Sel);
                                else
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDFW2R_Sel);
                                Thread.Sleep(300);
                                if (Robot2Msg.Instance.RobotBol)
                                {
                                    break;
                                }
                            }
                            Robot2Msg.Instance.CQWZInit = 1;
                            TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                            testDelegate.BeginInvoke(guid, null, null);
                        }
                        else
                        {
                            if (IPorPortInfo.Instance.CoffeeJB == 1)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB1R_UP);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB2R_UP);
                            Robot2Msg.Instance.RobotBol = false;
                            Thread.Sleep(300);
                            while (true)
                            {
                                if (IPorPortInfo.Instance.CoffeeJB == 1)
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDDJB1R_Sel);
                                else
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDDJB2R_Sel);
                                Thread.Sleep(300);
                                if (Robot2Msg.Instance.RobotBol)
                                {
                                    break;
                                }
                            }
                            if (IPorPortInfo.Instance.CoffeeJB == 1)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB1R_Down);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB2R_Down);
                            while (!instance.CFBol)
                            {
                                Thread.Sleep(100);
                            }
                            instance.CFBol = false;
                            if (IPorPortInfo.Instance.CoffeeJB == 1)
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(300);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB1ZC_start);
                                Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.JBInit2 * 1000));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB1ZC_end);
                                Thread.Sleep(300);
                                IOMsg.Instance.AllBol = true;
                            }
                            else
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(300);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB2YC_start);
                                Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.JBInit2 * 1000));
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB2YC_end);
                                Thread.Sleep(300);
                                IOMsg.Instance.AllBol = true;
                            }
                            instance.CFBol = true;
                            Thread.Sleep(300);
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJBWCR_UP);
                            Thread.Sleep(300);
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotCQDYDR_Down);
                            CHUC(guid);
                            Robot2Msg.Instance.CQWZInit = 0;
                            Thread.Sleep(300);
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJBWCR_Down);
                            while (!instance.CFBol)
                            {
                                Thread.Sleep(100);
                            }
                            instance.CFBol = false;
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            if (IPorPortInfo.Instance.CoffeeJB == 1)
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB1ZC_start);
                            else
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB2YC_start);
                            Thread.Sleep(3000);
                            if (IPorPortInfo.Instance.CoffeeJB == 1)
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB1ZC_end);
                            else
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB2YC_end);
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            instance.CFBol = true;
                        }
                        var cof = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                        if (cof != null)
                            Robot2Msg.Instance.RobotRQK(cqyd, qb, cof.LeachInit);
                        break;
                    case 2:
                        if (instance.coffeeInits.Any(a => a.Guid == guid && a.SWInit == 3))
                        {
                            if (Robot2Msg.Instance.CQWZInit == 0)
                            {
                                Thread.Sleep(300);
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotYDDCQR_UP);
                                Robot2Msg.Instance.RobotBol = false;
                                Thread.Sleep(300);
                                while (true)
                                {
                                    //Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQBWCR_Sel);
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDCQZBR_Sel);
                                    Thread.Sleep(300);
                                    if (Robot2Msg.Instance.RobotBol)
                                    {
                                        break;
                                    }
                                }
                                Thread.Sleep(300);
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotYDDCQR_Down);
                                Thread.Sleep(300);
                            }
                            var coffee = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                            Thread.Sleep(300);
                            if (coffee.ClassCodeInit != 3)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotPCQDJB12R_UP);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotPCQDJB3R_UP);
                            Thread.Sleep(300);
                            if (coffee.LeachInit == 1)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW1QBR_UP);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW2QBR_UP);
                            if (ClassCode == 3 && coffee.SugarInit == 1)
                            {
                                Thread.Sleep(300);
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJTR_UP);
                            }

                            Robot2Msg.Instance.RobotBol = false;
                            Thread.Sleep(300);
                            while (true)
                            {
                                if (coffee.LeachInit == 1)
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotBYNZ1R_Sel);
                                else
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotBYNZ2R_Sel);
                                Thread.Sleep(300);
                                if (Robot2Msg.Instance.RobotBol)
                                {
                                    var coffees = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                                    coffees.SWInit = 4;
                                    break;
                                }
                            }
                            Thread.Sleep(300);
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotYDDCQR_Down);
                            Thread.Sleep(300);
                            if (coffee.ClassCodeInit != 3)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotPCQDJB12R_Down);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotPCQDJB3R_Down);
                            if (coffee.LeachInit == 1)
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW1QBR_Down);
                            else
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQFW2QBR_Down);
                            Thread.Sleep(300);
                            Console.WriteLine(ClassCode + " chucan" + DateTime.Now.ToString());
                            if (ClassCode == 1 || ClassCode == 2)
                            {
                                Console.WriteLine(ClassCode + " chucankaishi" + DateTime.Now.ToString());
                                CHUC(guid);
                                Console.WriteLine(ClassCode + " chucanjieshu" + DateTime.Now.ToString());
                            }
                            else
                            {
                                if (coffee.SugarInit == 1)
                                {
                                    Robot2Msg.Instance.RobotBol = false;
                                    Thread.Sleep(300);
                                    while (true)
                                    {
                                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDDJTR_Sel);
                                        Thread.Sleep(300);
                                        if (Robot2Msg.Instance.RobotBol)
                                        {
                                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJTR_Down);
                                            break;
                                        }
                                    }
                                    while (!instance.CFBol)
                                    {
                                        Thread.Sleep(100);
                                    }
                                    instance.CFBol = false;
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(300);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GTB_start);
                                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.SugarInit1 * 1000));
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.GTB_end);
                                    Thread.Sleep(300);
                                    IOMsg.Instance.AllBol = true;
                                    instance.CFBol = true;
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJTWCR_UP);
                                    Thread.Sleep(300);
                                }
                                if (IPorPortInfo.Instance.CoffeeJB == 1)
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB1R_UP);
                                else
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB2R_UP);
                                Robot2Msg.Instance.RobotBol = false;
                                Thread.Sleep(300);
                                while (true)
                                {
                                    if (IPorPortInfo.Instance.CoffeeJB == 1)
                                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDDJB1R_Sel);
                                    else
                                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotDDJB2R_Sel);
                                    Thread.Sleep(300);
                                    if (Robot2Msg.Instance.RobotBol)
                                    {
                                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJTWCR_Down);
                                        Thread.Sleep(300);
                                        break;
                                    }
                                }
                                Thread.Sleep(300);
                                if (IPorPortInfo.Instance.CoffeeJB == 1)
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB1R_Down);
                                else
                                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotQJB2R_Down);
                                while (!instance.CFBol)
                                {
                                    Thread.Sleep(100);
                                }
                                instance.CFBol = false;
                                if (IPorPortInfo.Instance.CoffeeJB == 1)
                                {
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(300);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB1ZC_start);
                                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.JBInit1 * 1000));
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB1ZC_end);
                                    Thread.Sleep(300);
                                    IOMsg.Instance.AllBol = true;
                                }
                                else
                                {
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(300);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB2YC_start);
                                    Thread.Sleep(Convert.ToInt32(IPorPortInfo.Instance.JBInit1 * 1000));
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.JB2YC_end);
                                    Thread.Sleep(300);
                                    IOMsg.Instance.AllBol = true;
                                }
                                instance.CFBol = true;
                                Thread.Sleep(300);
                                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJBWCR_UP);
                                CHUC(guid);
                                while (!instance.CFBol)
                                {
                                    Thread.Sleep(100);
                                }
                                instance.CFBol = false;
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                if (IPorPortInfo.Instance.CoffeeJB == 1)
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB1ZC_start);
                                else
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB2YC_start);
                                Thread.Sleep(3000);
                                if (IPorPortInfo.Instance.CoffeeJB == 1)
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB1ZC_end);
                                else
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.SB2YC_end);
                                Thread.Sleep(200);
                                IOMsg.Instance.AllBol = true;
                                instance.CFBol = true;
                            }
                            Robot2Msg.Instance.CQWZInit = 0;
                        }
                        break;
                }
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// 出餐
        /// </summary>
        public void CHUC(string guid)
        {
            try
            {
                Thread.Sleep(300);
                int jbInit = 0;
                while (true)
                {
                    try
                    {
                        Console.WriteLine("kaishijiaobei " + DateTime.Now.ToString());
                        if (!WuLiaoCoffeeSel.Instance.Cup1_Sel_Bol)
                        {
                            jbInit = 1;
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB1R_UP);
                            break;
                        }
                        else if (!WuLiaoCoffeeSel.Instance.Cup2_Sel_Bol)
                        {
                            jbInit = 2;
                            Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB2R_UP);
                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(300);

                while (true)
                {
                    if (jbInit == 1)
                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB1WCR_Sel);
                    else
                        Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB2WCR_Sel);
                    Thread.Sleep(300);
                    if (Robot2Msg.Instance.RobotBol)
                    {
                        //if (jbInit == 1)
                        //    instance.CFCCBol1 = true;
                        //else
                        //    instance.CFCCBol2 = true;
                        break;
                    }
                }
                Thread.Sleep(300);
                if (jbInit == 1)
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB1R_Down);
                else
                    Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJB2R_Down);
                Thread.Sleep(300);
                Robot2SerialPort.Instance.SerialSend(Robot2Model.Instance.RobotJBWCR_Down);
                var cf = instance.coffeeInits.FirstOrDefault(a => a.Guid == guid);
                cf.TypeCodeInit = 3;
                ClientList clientList2 = new ClientList();
                WuliaoCoffeeInit init1 = new WuliaoCoffeeInit();
                init1.TypeCodeInit = 3;
                init1.Guid = guid;
                clientList2.coffeeInit = init1;
                clientList2.code = 5;
                if (SocketServer.Instance.CoffeeSocket != null)
                    SocketServer.Instance.CoffeeSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));

            }
            catch (Exception)
            {
                Console.WriteLine("报错了");
            }
        }
    }
}
